package com.hunttown.mes.convert;

import com.google.common.collect.Lists;
import com.hunttown.mes.common.utils.BeanUtils;
import com.hunttown.mes.domain.WorkflowBusinessNode;
import com.hunttown.mes.rpc.domain.WorkflowBusinessNodeDTO;
import com.hunttown.mes.rpc.domain.enums.ErrorCode;
import com.hunttown.mes.rpc.exception.ConvertException;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;

/**
 * created by wangjunfu on 2019-08-01 15:48:39
 */
public class WorkflowBusinessNodeConverts {

    private static Logger LOG = LoggerFactory.getLogger(WorkflowBusinessNodeConverts.class);

    public static List<WorkflowBusinessNodeDTO> convert(List<WorkflowBusinessNode> workflowBusinessNodeList) {
        if (CollectionUtils.isEmpty(workflowBusinessNodeList)) {
            return Collections.EMPTY_LIST;
        }

        List<WorkflowBusinessNodeDTO> workflowBusinessNodeListResult = Lists.newArrayList();
        for (WorkflowBusinessNode workflowBusinessNode : workflowBusinessNodeList) {
            workflowBusinessNodeListResult.add(convert(workflowBusinessNode));
        }
        return workflowBusinessNodeListResult;
    }

    public static WorkflowBusinessNodeDTO convert(WorkflowBusinessNode workflowBusinessNode) {
        try {
            return BeanUtils.copy(workflowBusinessNode, WorkflowBusinessNodeDTO.class);
        } catch (Exception e) {
            LOG.error("WorkflowBusinessNode到WorkflowBusinessNodeDTO转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "WorkflowBusinessNode到WorkflowBusinessNodeDTO转换失败", e);
        }
    }

    public static WorkflowBusinessNode convert(WorkflowBusinessNodeDTO workflowBusinessNodeDTO) {
        try {
            return BeanUtils.copy(workflowBusinessNodeDTO, WorkflowBusinessNode.class);
        } catch (Exception e) {
            LOG.error("WorkflowBusinessNodeDTO到WorkflowBusinessNode转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "WorkflowBusinessNodeDTO到WorkflowBusinessNode转换失败", e);
        }
    }
}